package com.example.blogsystem.mapper;

import com.example.blogsystem.model.Blog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface BlogMapper {
    @Select("select id, title, summary, create_time, file_flag  from blog where delete_flag != 1 order by id desc")
    public List<Blog> queryAllBlogs();

    @Select("select * from blog where id = #{id} and delete_flag != 1 ")
    public Blog queryBlogById(Integer id);

    @Update("update blog set delete_flag = 1 where id = #{id}")
    public void deleteBlogById(Integer id);

    @Update("update blog set title = #{title}," +
            " content = #{content}" +
            " where id = #{id}")
    public void updateBlog(Blog blog);

    @Insert("insert into blog (title, content, user_id, summary, file_flag) " +
            "values (#{title}, #{content}, #{userId}, #{summary}, #{fileFlag})")
    public void insertBlog(Blog blog);

    @Select("select count(*) as articleNumber from blog where user_id = #{id} and delete_flag = 0")
    public Integer countArticles(Integer id);
}
